package com.edu.edumanager.service.imp;

import com.alibaba.excel.EasyExcel;
import com.edu.edumanager.bean.EduSubject;
import com.edu.edumanager.mapper.EduSubjectMapper;
import com.edu.edumanager.vo.EduSubjectList;
import com.edu.edumanager.vo.ElementTreeVo;
import com.edu.edumanager.vo.ExcelVo;
import com.edu.edumanager.exception.EduException;
import com.edu.edumanager.listener.ExcelListener;
import com.edu.edumanager.service.ClassService;
import com.edu.edumanager.vo.EduResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-13 15:35
 **/
@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    EduSubjectMapper eduSubjectMapper;

    @Override
    public String findSubjectTitleById(Integer subjectId) {
        String title = eduSubjectMapper.findSubjectTitleById(subjectId);
        return title;
    }

    /**
     * 处理具体数据
     * @param file
     * @return
     */
    @Override
    public EduResult uploadExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),ExcelVo.class,new ExcelListener(eduSubjectMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new EduException("excle文件上传处理失败");
        }
        return EduResult.ok();
    }

    @Override
    public List<ExcelVo> selectAllSubject() {
        // 获取所有1类目录
        List<EduSubject> eduSubjectsOne =  eduSubjectMapper.selectAllSubject(0);
        Map<Integer,String> oneMap = new HashMap();
        if(eduSubjectsOne != null && eduSubjectsOne.size()!=0){
            for(int i=0;i<eduSubjectsOne.size();i++){
                oneMap.put(eduSubjectsOne.get(i).getId(),eduSubjectsOne.get(i).getTitle());
            }
        }
        List<ExcelVo> list = new ArrayList<>();
        List<EduSubject> eduSubjectsTwo =  eduSubjectMapper.selectAllSubject(-1);
        int parentId = 0;
        if(eduSubjectsTwo != null && eduSubjectsTwo.size()!=0){
            for (EduSubject subject : eduSubjectsTwo) {
                parentId = subject.getParentId();
                String oneName = oneMap.get(parentId);
                if (!oneName.equals("")){
                    list.add(new ExcelVo(oneName,subject.getTitle()));
                }
            }
        }



        return list;
    }

    @Override
    public List<ElementTreeVo> findSubjectByParentId(Integer parentId) {
        List<ElementTreeVo> result = new ArrayList<ElementTreeVo>();
        List<EduSubject> list = eduSubjectMapper.findSubjectByParentId(parentId);
        for (EduSubject subject : list) {
            ElementTreeVo vo = new ElementTreeVo();
            vo.setId(subject.getId());
            vo.setLabel(subject.getTitle());
            vo.setLeaf(subject.getParentId() == 0 ? true : false);
            result.add(vo);
        }
        return result;
    }

    /**
     * spring的申明式事务管理
     * 管理sql语句同时成功或者同时失败
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteSubject(Integer id) {
        //删除parentId 删除他的子集 还要删除自己本身
        int i = eduSubjectMapper.deleteSubjectByParentId(id);
        int j = eduSubjectMapper.deleteSubjectById(id);
        if (j == 0) {
            throw new EduException("删除课程分类失败");
        }
        return j;
    }

    @Override
    public String updateSubjectById(Integer id, String value) {
        EduSubject eduSubject = eduSubjectMapper.findSubjectByName(value);
        if (eduSubject!=null){
            throw new EduException("已有重复的课程");
        }
        eduSubject= new EduSubject();
        eduSubject.setId(id);
        eduSubject.setTitle(value);
        eduSubject.setGmtModified(new Date());
        int row = eduSubjectMapper.updateSubjectById(eduSubject);
        if (row==0){
            throw new EduException("课程修改失败");
        }
        return eduSubject.getTitle();
    }

    @Override
    public List<EduSubjectList> getSubjectList() {
        //现查出所有一级类目
        List<EduSubject> eduSubjectsOne = eduSubjectMapper.findSubjectByParentId(0);
        List<EduSubjectList> listResult = new ArrayList<EduSubjectList>();
        for (EduSubject one : eduSubjectsOne) {
            EduSubjectList subjectList1 = new EduSubjectList();
            subjectList1.setValue(one.getId());
            subjectList1.setLabel(one.getTitle());
            // 查对应的二级类目
            List<EduSubject> eduSubjectsTwo = eduSubjectMapper.findSubjectByParentId(one.getId());
            List<EduSubjectList> listResult2 = new ArrayList<EduSubjectList>();
            for (EduSubject two : eduSubjectsTwo) {
                EduSubjectList subjectList2 = new EduSubjectList();
                subjectList2.setLabel(two.getTitle());
                subjectList2.setValue(two.getId());
                listResult2.add(subjectList2);
                subjectList1.setChildren(listResult2);
            };
            listResult.add(subjectList1);
        }
        return listResult;
    }



}
